package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.Free;
import com.czh.springboot04.service.FreeService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("free")
public class FreeController {
    @Autowired
    private FreeService freeService;
    @PostMapping("/freeAdd")
    public R freeAdd(@RequestBody Free free){
        System.out.println(free);
        int i = freeService.freeAdd(free);
        if (i>0){
           return R.ok(i);
        }else {
            return R.fail("添加失败");
        }
    }
    @PutMapping("/freeUpdate")
    public R freeUpdate(@RequestBody Free free){
        int i = freeService.freeUpdate(free);
        if (i>0){
            return R.ok(i);
        }else {
            return R.fail("修改失败");
        }
    }
    @DeleteMapping("/freeDelete/{id}")
    public R freeDelete(@PathVariable int id){
        int i = freeService.freeDelete(id);
        if (i>0){
            return R.ok(i);
        }else {
            return R.fail("添加失败");
        }
    }
    @GetMapping("/freeSelectAll")
    public R freeSelectAll(@RequestParam(required = false, defaultValue =  "1") int pageNum,
                           @RequestParam(required = false, defaultValue =  "3")int pageSize ){
        PageInfo<Free> frees = freeService.freeSelectAll(pageNum, pageSize);
        if (frees!=null){
            return R.ok(frees);
        }else{
            return R.fail("查询失败");
        }
    }
    @GetMapping("/freeSelectByName")
    public R freeSelectByName(@RequestParam(required = false, defaultValue =  "1") int pageNum,
                              @RequestParam(required = false, defaultValue =  "3")int pageSize,
                              @RequestParam(required = false, defaultValue =  "")String name) {
        PageInfo<Free> frees = freeService.freeSelectByName(pageNum, pageSize, name);
        if (frees != null) {
            return R.ok(frees);
        } else {
            return R.fail("查询失败");
        }
    }

}
